import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'
import home from '../views/Home.vue'
import welcome from '../views/welcome.vue'
import users from '../views/user/users.vue'
import rights from '../views/power/rights.vue'
import roles from '../views/power/roles.vue'
import cate from '../views/goods/cate.vue'
import params from '../views/goods/params.vue'
import list from '../views/goods/list.vue'
import goods_add from '../views/goods/goods_add.vue'
import orders from '../views/orders/orders.vue'
import report from '../views/report/report.vue'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: './login'
    },
    {
        path: '/login',
        component: login
    },
    {
        path: '/home',
        component: home,
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                component: welcome
            },
            {
                path: '/users',
                component: users
            },
            {
                path: '/rights',
                component: rights
            },
            {
                path: '/roles',
                component: roles
            },
            {
                path: '/categories',
                component: cate
            },
            {
                path: '/params',
                component: params
            },
            {
                path: '/goods',
                component: list,

            },
            {
                path: '/goods/add',
                component: goods_add
            },
            {
                path: '/orders',
                component: orders
            },
            {

                path: '/reports',
                component: report
            }
        ]
    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next();
    const token = window.sessionStorage.getItem('token')
        //如果token等于空值强制跳转到登录页
        // console.log(token);
    if (!token) return next('/login');
    next()
})
export default router